// 登录
/* eslint-disable no-unused-vars */
let { addToken } = require("../token/addToken.js");
let {
    user
} = require("./sign_schma.js");

// let { userFind } = require("../user/userFind.js");
module.exports = {
    login: async function (req, res, err) {
        let loginData = req.query;
        console.log(loginData, "login");
        //登录时验证是否存在超管
        user.find({
            user: "loveyou"
        }).then((r) => {
            if (r.length == 0) {
                let root = {
                    user: "loveyou",
                    passwd: "loveyou",
                    email: "997884551@qq.com",
                    tel: "13169427675",
                    limit: 0,
                    limitMange: {
                        isImageMange: true,
                        isExpenseMange: true,
                        isControlMange: true,
                        isLove: true,
                        isDataMange: true,
                        isUserMange: true,
                        isChatMange: true,
                        isEmailMange: true,
                    },
                    createTime: "1970-01-01 00:00:00",
                    lastTime: "1970-01-01 00:00:00"
                };
                user.create(root);
            }
        })

        let len = await user.countDocuments({ user: loginData.user });

        if (len > 0) {
           await addToken({ user:loginData.user, createTime:loginData.newDate, outTime:30 * 60 * 1000 });
            user.find({ user: loginData.user }).then((dataUser) => {
                res.send({
                    data: dataUser
                })
            })
        } else {
            res.send({
                data: []
            })
        }
    }
}